enum Color
{
    Red,
    Blue,
    Green
}


enum PAY_STATUS 
{
    NO_PAY = "未支付",
    PAY = "支付",
    SUCCESS = "交易成功"
}

//类型别名
type color = Color|string|null;

let clothesColor:color = Color.Blue;
//联合类型（Union Types）
let payDesc:PAY_STATUS|string|number = PAY_STATUS.PAY;
payDesc = "未支付"
payDesc = 0.1;


//类型断言
let stu_name:any = null;
let len:number = stu_name?.length ?? 10;

//非空断言
//let str1:string|null = null;

// function foo(name:string|null)
// {
//     let strLen:number = name?.length ?? 10;
//     return strLen
// }

//类型保护 - typeof
let foo1 = function(name:string|null):string
{
     let strLen:number = name?.length ?? 10;
     return String(strLen)
}

console.log(typeof foo1,typeof foo1("zhangsan"));

//类型保护 -  instanceof
class Animal{
   name?:string;
   age?:number;
}

let dog = new Animal();
dog.name = "dahuang";
console.log(dog instanceof Animal);

type keys = keyof Animal;

// 泛型函数，K 必须是 T 的键之一
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
  return obj[key];
}

console.log(getProperty(dog,"name"));

//Person对象，要求遍历Person对象的所有参数的值
class Person
{
    name:string = '';
    age:number = 0;
    sex:boolean = false;
    className:string = '';
    teacher:string = '';
}

let obj1 = new Person();
obj1.name = "zhangshan";
obj1.age = 18;
obj1.sex = true;
obj1.className = "22届A班";
obj1.teacher = "李老师";

//要求遍历obj1对象的所有的属性值
let entity:object[] = Object.entries(obj1);
entity.forEach(e =>{
   let pname = Object.values(e)[0];
   let value =  Object.values(e)[1];
   console.log(`属性名:${pname} -> ${value}`);
})

//[ 'name', 'age', 'sex', 'className', 'teacher' ]
